#pragma once
#ifndef _ASIO_SERVER_H_
#define _ASIO_SERVER_H_

#include "Netbase.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using namespace boost;
using namespace boost::asio;

namespace XGC
{
	namespace net
	{
		class asio_NetworkHandler;
		typedef shared_ptr< asio_NetworkHandler > asio_NetworkHandlerPtr;
		struct asio_ServerBase
		{
			virtual bool StartServer( const char *address, short port ) = 0;
			virtual void StopServer() = 0;
		};

		class asio_Server	:	public asio_ServerBase
		{
		public:
			asio_Server( IMessageQueue* pMessageQueue, _uint32 timeout = 0 );
			~asio_Server(void);

			bool StartServer( const char *address, short port );
			void StopServer();

		protected:
			void handle_accept( asio_NetworkHandlerPtr pNetHandler, const system::error_code& error);
			void handle_stop();
		private:
			io_service service_;
			ip::tcp::acceptor acceptor_;
			_uint32 timeout_;
			IMessageQueue *m_pMessageQueue;
		};

		class asio_ServerEx	:	public asio_ServerBase
		{
		public:
			asio_ServerEx( create_handler_func call, long_ptr param, _uint32 timeout = 0 );
			~asio_ServerEx(void);

			bool StartServer( const char *address, short port );
			void StopServer();

		protected:
			void handle_accept( asio_NetworkHandler *pNetHandler, const system::error_code& error);
			void handle_stop();

		private:
			io_service service_;
			ip::tcp::acceptor acceptor_;
			create_handler_func call_;
			long_ptr param_;
			_uint32 timeout_;
		};

	}
}
#endif // _ASIO_SERVER_H_